#include "mark.h"

#include <QDebug>

Mark::Mark(QWidget *parent)
    : QDialog(parent)
{
    QSettings openFile("usrInformation.ini", QSettings::IniFormat);
    openFile.setIniCodec(QTextCodec::codecForName("UTF-8"));
    int colorSaved = openFile.value("usr/markColor").toInt();

    hide();
    setWindowFlag(Qt::WindowStaysOnTopHint);
    setWindowFlag(Qt::FramelessWindowHint);

    qDebug() << 1;

    resize(240, 320);
    setStyleSheet(tr("QDialog{background-color:%1}").arg(markColor[colorSaved]));

    btnClose = new QPushButton(this);
    btnClose->setStyleSheet("QPushButton{border-image:url(:img/close.png)}"
                            "QPushButton:hover{border-image:url(:img/close-hover.png)}");
    btnClose->resize(30, 30);
    btnClose->move(width() - 30 - 5, 5);

    plainTextEdit = new QPlainTextEdit(this);
    plainTextEdit->setStyleSheet(tr("QPlainTextEdit{background-color:%1;font-size:%2;"
                                    "font-family:Consolas;border-width:100px;}").arg(markColor[colorSaved]).arg(myFontSize));
    plainTextEdit->verticalScrollBar()->setStyleSheet(tr("QScrollBar:vertical{margin:0px 0px 0px 0px;border:0px;width:12px}"
                                                      "QScrollBar::handle:vertical{background-color:rgb(180, 180, 180);width:12px;border-radius:3px;border-width:2px}"
                                                      "QScrollBar::add-page:vertical{background-color:%1}"
                                                      "QScrollBar::sub-page:vertical{background-color:%1}"
                                                      "QScrollBar::handle:vertical:pressed{background-color:rgb(225,225,225)}").arg(markColor[colorSaved % 10]));

    QFont font("Consolas", 8);
    toolBar = new QToolBar(this);
    action_1 = new QAction(this);
    action_1->setText(tr("加粗"));
    action_1->setCheckable(true);
    action_1->setFont(font);
    action_2 = new QAction(this);
    action_2->setText(tr("字体+"));
    action_2->setFont(font);
    action_3 = new QAction(this);
    action_3->setText(tr("字体-"));
    action_3->setFont(font);

    toolBar->addAction(action_1);
    toolBar->addAction(action_2);
    toolBar->addAction(action_3);
    toolBar->setStyleSheet(tr("QToolBar{background-color:%1;color:rgb(204,204,204)}").arg(markColor[colorSaved]));

    openFile.setValue("usr/markColor", (colorSaved + 1) % 10);

    layout = new QVBoxLayout(this);
    layout->addWidget(plainTextEdit);
    layout->addWidget(toolBar);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 40, 0, 0);

    setLayout(layout);

    btnClose->raise();
    connect(btnClose, &QPushButton::clicked, this, &Mark::closeWindow);
    connect(action_1, &QAction::triggered, this, &Mark::setMyBold);
    connect(action_2, &QAction::triggered, this, &Mark::setMyFontSizeAdd);
    connect(action_3, &QAction::triggered, this, &Mark::setMyFontSizeSub);
}

Mark::~Mark()
{
}

void Mark::closeWindow()
{
    reject();
}

void Mark::setMyBold()
{
    myBold = 1 - myBold;
    if (myBold)
    {
        QFont font("Consolas", myFontSize, QFont::Bold);
        plainTextEdit->setFont(font);
    }
    else
    {
        QFont font("Consolas", myFontSize, QFont::Normal);
        plainTextEdit->setFont(font);
    }
}

void Mark::setMyFontSizeAdd()
{
    if (myFontSize < 30)
    {
        myFontSize += 2;
        qDebug() << myFontSize;
        plainTextEdit->setFont(QFont(tr("Consolas"), myFontSize));
    }
}

void Mark::setMyFontSizeSub()
{
    if (myFontSize > 6)
    {
        myFontSize -= 2;
        qDebug() << myFontSize;
        plainTextEdit->setFont(QFont(tr("Consolas"), myFontSize));
    }
}

void Mark::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_move = true;
        m_startPoint = event->globalPos();
        m_windowPoint = this->frameGeometry().topLeft();
    }
}
void Mark::mouseMoveEvent(QMouseEvent *event)
{
    if (m_move)
    {
        QPoint relativePos = event->globalPos() - m_startPoint;
        this->move(m_windowPoint + relativePos);
    }
}
void Mark::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_move = false;
    }
}
